package kr.hkit.androidcomplete02.ch16;

import kr.hyunist.androidcomplete01.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class OrderDialogEx extends Activity implements CustomDialogFragment.NoticeDialogListener {
	TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_dialog);
		tv = (TextView) findViewById(R.id.ch16_text);
	}

	@SuppressWarnings("unused")
	public void mOnClick(View v){
		CustomDialogFragment cdf = new CustomDialogFragment();
		cdf.show(getFragmentManager(), "dialog");
		
		
		
	}

	@Override
	public void onDialogClick(DialogFragment dialog, String res) {
		tv.setText("주문 내역 : " + res);
	}

}

class CustomDialogFragment extends DialogFragment {
	NoticeDialogListener mListener;
	Bundle bundle;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (NoticeDialogListener) activity;
		} catch (ClassCastException e) {
			String msg = "뭐래뭐래";
			throw new ClassCastException(activity.toString() + msg);
		}
	}

	public interface NoticeDialogListener {
		public void onDialogClick(DialogFragment dialog, String res);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		bundle = getArguments();
		//bundle.putString("title", "알립니다");
		LayoutInflater inflater = getActivity().getLayoutInflater();
		final LinearLayout linear = (LinearLayout) inflater.inflate(R.layout.ch16_order, null);

		return new AlertDialog.Builder(getActivity())
				.setIcon(R.drawable.ic_launcher)
				/*.setTitle(bundle.getString("title"))*/.setView(linear)
				.setPositiveButton("확인", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						EditText product = (EditText) linear.findViewById(R.id.ch16_product);
						EditText number = (EditText) linear.findViewById(R.id.ch16_number);
						CheckBox paymethod = (CheckBox) linear.findViewById(R.id.ch16_paymethod);
						StringBuilder sb = new StringBuilder("주문 정보\n");
						sb.append(product.getText() + " ");
						sb.append(number.getText() + "개 ");
						sb.append(paymethod.isChecked() ? "착불결제" : "");
						mListener.onDialogClick(CustomDialogFragment.this, sb.toString());
					}
				}).setNegativeButton("취소", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						mListener.onDialogClick(CustomDialogFragment.this, "주문을 취소했습니다.");
					}
				}).create();
	}
}